package com.zygh.questionnairesurvey.controller;

import com.alibaba.fastjson.JSONObject;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.Dept;
import com.zygh.questionnairesurvey.entity.ProblemDetails;
import com.zygh.questionnairesurvey.server.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/Dept")
@Api(tags = "部门管理")
public class DeptController {

    @Resource
    private DeptService deptService;


    @GetMapping("list")
    @ApiOperation(value = "查询所有部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部門id", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", required = true)
    })
    public CommonResult listAll(Integer id,
                                @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return deptService.listAll(id, pageNum, pageSize);
    }


    @GetMapping("getById")
    @ApiOperation(value = "根据id查询部门信息")
    public CommonResult getById(Integer deptId) {
        return deptService.getById(deptId);
    }


    @PostMapping("insert")
    @ApiOperation(value = "新增部门")
    public CommonResult insert(@RequestBody Dept dept) {
        return deptService.insert(dept);
    }


    @PostMapping("update")
    @ApiOperation(value = "修改部门")
    public CommonResult update(@RequestBody Dept dept) {
        return deptService.update(dept);
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除部门")
    public CommonResult delete(@RequestBody String id) {
        String str = JSONObject.parseObject(id).get("id").toString();
        Integer new_id = Integer.parseInt(str);
        return deptService.delete(new_id);
    }

    @GetMapping("findDeptByid")
    @ApiOperation(value = "根據id查询下级部门")
    public CommonResult findDeptByid(Integer id, Integer pageNum, Integer pageSize) {
        return deptService.findDeptByid(id, pageNum, pageSize);
    }

}